package com.crazy.mybatis.gerenator.service.xml;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import com.crazy.mybatis.gerenator.service.Context;
import com.crazy.mybatis.gerenator.service.common.ElementName;

@Component
public class BaseColumnBuilder extends AbstractElementBuilder implements ElementBuilder {

	private static final String DEFAULT_ID = "Base_Column_List";
	
	public BaseColumnBuilder() {
		this(DEFAULT_ID, ElementName.SQL);
	}
	public BaseColumnBuilder(String id, String elementName) {
		super(id, elementName);
	}

	@Override
	public Element build(Context context) {
		Element baseColumn = DocumentHelper.createElement(this.elementName);
		baseColumn.addAttribute("id", this.id);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < context.getColumnList().size(); i++) {
			String column = context.getColumnList().get(i);
			sb = sb.append(column).append(", ");
			if (i % 5 == 4) {
				sb = sb.append("\n\t\t");
			}
		}
		sb = sb.deleteCharAt(sb.lastIndexOf(","));
		baseColumn.setText(sb.toString().trim());
		return baseColumn;
	}

}
